package com.pocket.insight.controller;

import com.pocket.insight.base.context.OffsetLimitContext;
import com.pocket.insight.base.dto.Result;
import com.pocket.insight.controller.dto.*;
import com.pocket.insight.entity.Connection;
import com.pocket.insight.entity.Dataset;
import com.pocket.insight.service.ConnectionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author shuize
 * @create 2024/11/20
 */
@Tag(name = "数据连接")
@RestController
@RequestMapping("/connection")
public class ConnectionController {

    @Resource private ConnectionService connectionService;

    @PostMapping("verify")
    @Operation(summary = "校验connection")
    public Result<Connection.MetaConfig> verify(@RequestBody ConnectionOptionsDto optionsDto) {
        return Result.success(connectionService.verify(optionsDto));
    }

    @PostMapping
    @Operation(summary = "新增connection")
    public Result<Connection> save(@RequestBody Connection connection) {
        return Result.success(connectionService.save(connection));
    }

    @GetMapping
    @Operation(summary = "列表connection")
    public Result<List<Connection>> list() {
        return Result.success(connectionService.list());
    }

    @DeleteMapping("{id}")
    @Operation(summary = "删除connection")
    public Result delete(@PathVariable Long id) {
        connectionService.delete(id);
        return Result.success();
    }

    @GetMapping("{id}")
    @Operation(summary = "查看connection详情")
    public Result<Connection> detail(@PathVariable Long id) {
        return Result.success(connectionService.getById(id));
    }

    @PostMapping("{id}/path-tables")
    @Operation(summary = "根据id查询connection下所有路径的所有table")
    public Result<List<PathTableNode>> pathTables(@PathVariable Long id, @RequestParam(required = false) Boolean pathOnly, @RequestParam(required = false) List<String> path) {
        Connection connection = this.connectionService.getById(id);
        List<PathTableNode> pathTables = this.connectionService.getPathTables(connection, pathOnly, path);
        return Result.success(pathTables);
    }

    @PostMapping("{id}/path-table-data")
    @Operation(summary = "根据id查询table中的数据")
    public Result<DatasetResultDto> pathTableData(@PathVariable Long id, @RequestBody DatasetOptionsDto datasetPreviewDto) {
        Connection connection = this.connectionService.getById(id);
        datasetPreviewDto.setType(Dataset.Options.DatasetTypeEnum.connection);
        datasetPreviewDto.setConnectionId(id);
        Dataset dataset = new Dataset(datasetPreviewDto);
        SqlParam sqlParam = new SqlParam();
        sqlParam.offset = OffsetLimitContext.getOffset(Long.valueOf(0L));
        sqlParam.limit = OffsetLimitContext.getLimit(Long.valueOf(1000L));
        DatasetResultDto tableData = this.connectionService.previewDataAndSchema(connection, dataset, sqlParam);
        return Result.success(tableData);
    }

}
